package com.teckteam.ptvsystem.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Reusable JPanel class for selecting journey location point.
 * Currently supports selection from a Stop list, or GPS coordinates.
 * Search functionality is provided to help users.
 *
 */
public class JourneyChoicePanel extends JPanel
{
   private JComboBox cmBox;
   private JLabel dirLabel, latLabel, lonLabel, nameLocLabel;
   private JPanel listPanel, gpsPanel, btnPanel;
   private JButton findButton;
   private ButtonGroup btnGroup;
   private JRadioButton listBullet, gpsBullet;
   private JTextField latTextBox, lonTextBox, 
                      nameLocationTextBox;
   private boolean listMode;
   private Dimension numFieldSize = new Dimension(90, 20);
   private JTabbedPane tabPane;
   
   /**
    * Constructor for single choice panel for Journey Planner
    * 
    * @param jpDialog
    * @param stopList
    * @param direction
    */
   public JourneyChoicePanel(final JourneyPlanner jpDialog, ArrayList<String> stopList, 
         String direction)
   {
      setLayout(new BorderLayout());
      setBorder(BorderFactory.createLineBorder(Color.BLACK));
      dirLabel = new JLabel(direction);
      dirLabel.setBorder(BorderFactory.createRaisedBevelBorder());
      latLabel = new JLabel("Latitude: ");
      lonLabel = new JLabel("Longitude: ");
      nameLocLabel = new JLabel("Location name");
      cmBox = new JComboBox();
      listPanel = new JPanel();
      gpsPanel = new JPanel();
      tabPane = new JTabbedPane();
      latTextBox = new JTextField();
      lonTextBox = new JTextField();
      nameLocationTextBox = new JTextField();
      findButton = new JButton("<html><center>Search<br>for your<br>stop");
      listMode = true;
      tabPane.addChangeListener(new ChangeListener()
      {
         
         public void stateChanged(ChangeEvent e)
         {
            switch(tabPane.getSelectedIndex())
            {
               case 0:
                  setListPanel();
                  break;
               case 1:
                  setGPSPanel();
                  break;
            }
         }
         
      });
      
      //Differentiating Find action based on currently displayed mode
      findButton.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
            if(listMode)
            {
               System.out.println("-LISTMODE POPUP");
               new StopSearchPrompt(jpDialog, JourneyChoicePanel.this);
               
            } else
            {
               System.out.println("-GPSMODE POPUP");
               new GeocodePrompt(jpDialog, JourneyChoicePanel.this);
            }
         }
      });
      
      tabPane.addTab("Stop List", listPanel);
      tabPane.addTab("GPS coords", gpsPanel);
      
      listPanel.add(cmBox);
      latTextBox.setPreferredSize(numFieldSize);
      lonTextBox.setPreferredSize(numFieldSize);
      nameLocationTextBox.setPreferredSize(new Dimension(250, 20));
      
      gpsPanel.add(latLabel);
      gpsPanel.add(latTextBox);
      gpsPanel.add(lonLabel);
      gpsPanel.add(lonTextBox);
      gpsPanel.add(nameLocLabel);
      gpsPanel.add(nameLocationTextBox);      
      
      cmBox.setPreferredSize(new Dimension(350,25));
      cmBox.addItem("-- Select or find your desired location --");
      for (int i = 0; i < stopList.size(); i++)
         cmBox.addItem(stopList.get(i));
      
      dirLabel.setPreferredSize(new Dimension(32, 5));
      
      this.add(dirLabel, BorderLayout.WEST);
      this.add(tabPane, BorderLayout.CENTER);
      this.add(findButton, BorderLayout.EAST);
   }
   
   /**
    * Return either selected stop, or custom stop details
    * @return
    */
   public String getSelected()
   {
      if(listMode)
      {
         if(cmBox.getSelectedIndex()==0) 
            return "";
            
         return cmBox.getSelectedItem().toString();
      } else
      {
         return String.format("\1%s:%s:%s", latTextBox.getText(),
               lonTextBox.getText(), nameLocationTextBox.getText());
      }
   }
   
   private void setGPSPanel()
   {
      if(!listMode) return;
      System.out.println("GPSPanel Selected");
      listMode = false;
   }
   
   private void setListPanel()
   {
      if(listMode) return;
      System.out.println("ListPanel Selected");
      listMode = true;
   }
   
   /**
    * Passing from GPS prompt to pre-fill selected coordinates found.
    * 
    * @param lat
    * @param lon
    * @param name
    */
   protected void setGPSCoords(String lat, String lon, String name)
   {
      //TODO
   }
}
